package chapter09plus

/**
 *
 * @author cdhuangchao3
 * @date 2023/6/11 5:37 PM
 */
object Test02_Implicit {
  def main(args: Array[String]): Unit = {
    val new12 = new MyRichInt(12)
    println(new12.myMax(15))

    // 1.隐式函数
    implicit def convert(num: Int): MyRichInt = new MyRichInt(num)

    println(12.myMax(15))

    println("=" * 20)

    // 2.隐式类
    implicit class MyRichInt2(val self: Int) {
      // 自定义比较大小的方法
      def myMax2(n: Int): Int = if (n < self) self else n

      def myMin2(n: Int): Int = if (n < self) n else self
    }
    println(12.myMax2(15))

    // 3.隐式参数
    implicit val str: String = "ls"
//    implicit val str2: String = "ls"
    implicit val num: Int = 18

    def sayHello(implicit name: String = "zs"): Unit = {
      println(s"hello, ${name}")
    }

    def sayHi()(implicit name: String = "zs"): Unit = {
      println(s"hi, ${name}")
    }

    sayHello("zsf")
    sayHello()
    sayHi()
    sayHi()()
    sayHello

    // 简写
    def hiAge(): Unit = {
      println("hi" + implicitly[Int])
    }
    hiAge()
  }
}

// 自定义类
class MyRichInt(val self: Int) {
  // 自定义比较大小的方法
  def myMax(n: Int): Int = if (n < self) self else n

  def myMin(n: Int): Int = if (n < self) n else self
}